<?php

/**
 * This is the model class for table "{{assignment}}".
 *
 * The followings are the available columns in table '{{assignment}}':
 * @property integer $ID
 * @property integer $assignee
 * @property integer $assigner
 * @property integer $action
 * @property integer $result
 * @property integer $item_type
 * @property integer $itemID
 * @property integer $createddate
 */
class Assignment extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Assignment the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{assignment}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('assignee, assigner, action, item_type, itemID, createddate', 'required'),
			array('assignee, assigner, result, item_type, itemID, createddate', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('ID, assignee, assigner, action, result, item_type, itemID, createddate', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ID' => 'ID',
			'assignee' => 'Assignee',
			'assigner' => 'Assigner',
			'action' => 'Hành động',
			'result' => 'Kết quả thực hiện',
			'item_type' => 'Item Type',
			'itemID' => 'Item',
			'createddate' => 'Ngày tạo',
		);
	}		
	
	protected function beforeSave() {		
		$this->item_type = 1;		
		$this->createddate = time(); //dung hay sai o day vay e?
		return parent::beforeSave();
	}
	
			
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('ID',$this->ID);
		$criteria->compare('assignee',$this->assignee);
		$criteria->compare('assigner',$this->assigner);
		$criteria->compare('action',$this->action);
		$criteria->compare('result',$this->result);
		$criteria->compare('item_type',$this->item_type);
		$criteria->compare('itemID',$this->itemID);
		$criteria->compare('createddate',$this->createddate);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
/**
	 * @author khatv
	 */
	public function updateAssignment($resolverID,$itemID,$action)
	{	
		$assignment = Assignment::model()->findByAttributes(array('assignee'=>$resolverID,'itemID'=>$itemID));
		if(!empty($assignment))
		{						
			$assignment->assigner =  Yii::app()->user->id;
			$assignment->action =  $action;			
			$assignment->update();
		}
		else
		{
			$assignment = new Assignment();
			$assignment->assignee =  $resolverID; 
			$assignment->assigner =  Yii::app()->user->id;
			$assignment->action =  $action;			
			$assignment->itemID = $itemID;		
			$assignment->save(false);
		}		
	}
}